//
//  CZEmotionPageViewCell.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZEmotionPageViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 删除表情按钮的监听
    func deleteBtnClick(){
        
//        print("删除按钮")
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEMOTICONDELETEBUTTONNOTIFICATION, object: nil)
        
    }
    
    //表情点击的监听
    func emoticonBtnClick(button:CZEmoticonButton){
        
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEMOTICONBUTTONNOTIFICATION, object: button.emoticonModel)
        
    }
    
    
    //定义一个属性，供外界调用
    var emoticons:[CZEmoticonModel]?{
        
        didSet{
            
            for buttons in buttonsArray {
                buttons.hidden = true
            }
            
            // 遍历emoticons 
            for (i,emoticonModel) in emoticons!.enumerate() {
                
                //获取buttons
                let button = buttonsArray[i]
                //给button的模型赋值
                button.emoticonModel = emoticonModel
                
                button.hidden = false
                
                //判断是不是emoji表情，是的话要设置button的title
                if emoticonModel.isEmoji {
                    let code = (emoticonModel.code ?? "" as NSString).emoji()
                    
                    button.setTitle(code, forState: UIControlState.Normal)
                    button.setImage(nil, forState: UIControlState.Normal)
                }else{
                   
                    //图片表情，需要设置image
                    
                    button.setImage(UIImage.emoticonsImage(emoticonModel), forState: UIControlState.Normal)
                    button.setTitle(nil, forState: UIControlState.Normal)
                    
                }
                
            }
            
            
            
        }
        
    }
    
    
    //设置TEXT
    var indexpath:NSIndexPath?{
        
        didSet{
         
            messageLabel.text = "第\(indexpath?.section ?? 0)组\n第\(indexpath?.row ?? 0)行"
            
        }
    }
    
    
    //定义一个可变的空数组
    var buttonsArray: [CZEmoticonButton] = [CZEmoticonButton]()
    
    //MARK: - 设置UI
    func setupUI(){
        backgroundColor = RandomColor()
        //添加控件
//        contentView.addSubview(messageLabel)
       
//        //设置UI
//        messageLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(contentView)
       
//        }
        //添加按钮
        addButtons()
        
        //添加删除按钮
        contentView.addSubview(deleteButton)

    
    }
    
    
    //MARK: - 计算20 个按钮的宽
    override func layoutSubviews(){
            super.layoutSubviews()
         // 计算button的宽和高
        let w = (self.frame.width - 10)/CGFloat(CZEmoticonMaxCol)
        let h = (self.frame.height - 20)/CGFloat(CZEmoticonMaxRow)
        
        for (index,button) in buttonsArray.enumerate() {
           let col = CGFloat(index%CZEmoticonMaxCol)
            let row = CGFloat(index/CZEmoticonMaxCol)
            
            button.frame = CGRect(x: col*w + 5, y: row*h, width: w, height: h)
        }
        //给删除按钮设置frame
        deleteButton.frame = CGRect(x: self.frame.width - 5 - w, y: h * 2, width: w, height: h)
        
    
    }
    
    //MARK: - 创建20个按钮
    func addButtons(){
        
        //循环创建button
        
        for _ in 0..<CZEmoticonMaxCount {
            let button = CZEmoticonButton()
            
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            //添加点击事件
            button.addTarget(self, action: "emoticonBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            //添加button
            buttonsArray.append(button)
            contentView.addSubview(button)
            
        }
        
    }
    
    
    
    //MARK: - 懒加载控件
    
    private lazy var messageLabel:UILabel = UILabel(textColor: UIColor.blueColor(), textSize: 15, maxWidth: SCREENW)
    
    //加载删除按钮
    private lazy var deleteButton:UIButton = {
       
        let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: "deleteBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named:"compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
}







